<!--
 * @Author: daidai
 * @Date: 2022-02-20 16:16:42
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-25 09:18:22
 * @FilePath: \web-pc\src\pages\big-screen\view\indexs\left-center.vue
-->
<template>
  <div>
    <div class="box-bar">
      <div class="box" v-for="(item, idx) in barList" :key="idx" @click="changeChart(idx)">
        <div class="mask" :class="idx == barChoose ? 'actived' : ''"></div>
        {{ item }}
      </div>
    </div>
    <Echart id="leftCenter" v-if="pageflag" :options="options" isClick class="left_center_inner" ref="charts"
      height="220px" @chartClick="chartClick" />
    <Reacquire v-else @onclick="getData" style="line-height: 200px">
      重新获取
    </Reacquire>
  </div>

</template>

<script>
import { currentPOST } from 'api/modules';
import { colorsA, colorsB, legend, xAxis, yAxis } from '@/config/EChartVar';
import { graphic } from 'echarts';

export default {
  data() {
    return {
      barList: ['票数', '重量', '体积', '运费'],
      chartType: 0,
      barChoose: 0,
      barTitle: '票数',
      options: {},
      tag: 1,
      countUserNumData: {
        axisData: ['2021', '2022', '2023', '2024', '2025'],
        data0: [4184, 4451, 5232, 5312, 65],
        data1: [8300, 8500, 8700, 8800, 2600],
        data2: [25100, 26701, 29016, 31321, 8800],
        data3: [36, 39, 44, 42, 5],
        data4: [120, 160, 150, 160, 170],
      },
      pageflag: true,
      timer: null,
      chartType: 0,
      autoTimer: null,
      series: [],
    };
  },
  created() {
    this.getData();
  },
  mounted() { },
  beforeDestroy() {
    this.clearData();
  },
  methods: {
    autoChange() {
      if (this.barChoose >= 3) {
        this.barChoose = 0;
      } else {
        this.barChoose += 1;
      }
      this.changeChart(this.barChoose)
    },
    clearData() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
    getData() {
      this.pageflag = true;

      this.$nextTick(() => {
        this.init(this.countUserNumData, this.barChoose);

        this.autoSwitper();
      });
      // this.switper();
    },
    changeChart(chartType) {
      this.barChoose = chartType;
      this.options.legend.data[0] = this.barList[chartType];
      this.options.series[0].name = this.barList[chartType];
      this.barTitle = this.barList[chartType];
      this.init(this.countUserNumData, chartType)
    },
    //轮询
    switper() {
      if (this.timer) {
        return;
      }
      let looper = (a) => {
        this.getData();
      };
      this.timer = setInterval(
        looper,
        2000 // this.$store.state.setting.echartsAutoTime // staticAutoTime
      );
      let myChart = this.$refs.charts.chart;
      myChart.on('mouseover', (params) => {
        this.clearData();
      });
      myChart.on('mouseout', (params) => {
        this.timer = setInterval(
          looper,
          2000 // this.$store.state.setting.echartsAutoTime // staticAutoTime
        );
      });
    },
    autoSwitper() {
      if (this.autoTimer) {
        return;
      }
      let looper = (a) => {
        this.autoChange();
      };
      this.autoTimer = setInterval(
        looper,
        3000
      );
    },
    init(currentData, barChoose) {
      // let axisData = ["2021年", "2022年", "2023年", "2024年"];
      let axisData = currentData.axisData;
      let seriesData = [
        [...currentData.data0],
        [...currentData.data1],
        [...currentData.data2],
        [...currentData.data3],
        [...currentData.data4],
      ];

      const options = {
        // backgroundColor: "#0F224C", //背景色
        grid: {
          top: '15%',
          left: '4%',
          right: '8%',
          bottom: '12%',
          containLabel: true,
        },
        legend: {
          show: true,
          top: '0%',
          right: '4%',
          icon: 'circle',
          itemWidth: 12,
          itemHeight: 12,
          // itemGap: 60,
          textStyle: legend.textStyle,
          data: ['万票', '万吨', '万方', '万元'],
        },
        xAxis: {
          type: 'category',
          data: axisData,
          boundaryGap: false,
          // boundaryGap: true,
          splitLine: {
            show: false,
          },
          axisTick: {
            show: false, // 不显示下切线
          },
          axisLabel: xAxis.axisLabel,
          axisLine: {
            lineStyle: {
              background: 'rgba(176, 215, 255, 0.4)',
            },
          },
        },
        yAxis: [
          {
            type: 'value',
            // max: 800,
            // min: 0,//
            axisLabel: yAxis.axisLabel,
            axisLine: {
              show: false,
              // interval: 1,
              lineStyle: {
                color: 'rgba(0,0,0,0.5)',
              },
            },
            splitLine: {
              show: true,
              // interval: 1,
              lineStyle: {
                color: ['rgba(176, 215, 255, 0.2)'],
                width: 1,
                type: 'dashed',
              },
            },
            // splitLine: {
            //   show: false,
            // },
            axisTick: {
              show: false,
            },
          },
          // {
          //   min: 0,
          //   max: 100,
          //   interval: 20,
          //   splitLine: { show: false },
          //   axisLine: {
          //     lineStyle: {
          //       color: '#B4B4B4',
          //     },
          //   },

          //   axisLabel: {
          //     formatter: '{value}',
          //   },
          // },
        ],
        series: [],
      };
      const series = [
        {
          name: (['万票', '万吨', '万方', '万元', '万票'])[this.barChoose],
          type: 'line',
          // 动画配置
          animation: true,
          animationDuration: 2000,
          animationEasing: 'quarticInOut',
          animationDelay: function (idx) {
            return idx * 150;
          },
          smooth: false,
          showSymbol: true,
          // symbol: 'circle',
          // symbolSize: 6,
          data: seriesData[barChoose],
          itemStyle: {
            normal: {
              // color: "#38FFBA",
              color: 'rgba(0,219,254, .7)',
            },
          },
          areaStyle: {
            color: colorsB[0],
          },
          lineStyle: {
            normal: {
              width: 2,
            },
          },
          label: {
            show: false,
            position: 'top',
            formatter: (par) => {
              return par.value;
            },
            textStyle: { fontSize: 12, color: '#FFF' },
          },
          markPoint: {
            symbol: 'circle',
            symbolSize: [14, 14], // 容器大小
            symbolOffset: ['0%', '0%'],
            data: [
              {
                name: '最大值',
                type: 'max',
                valueIndex: 1,
              },
            ],
            label: {
              show: true,
              position: 'top',
              fontSize: 12,
              color: 'rgba(0,219,254, 1)',
              formatter: (par) => {
                return par.value;// + (['万票', '万吨', '万方', '万亿', '万票'])[this.barChoose];
              },
            },
          },
        },
      ];
      options.series = series;
      this.options = options;
    },
    chartClick(params) {
      console.log('点击的数据:', params);
      const idx = this.countUserNumData['data' + this.barChoose].findIndex(item => item === params.value)
      const name = this.countUserNumData.axisData[idx]
      // 这里处理你的点击逻辑
      var title = this.barTitle ? '货量规模（' + this.barTitle + '）' : '货量规模';
      this.$emit('chartClick', { ...params, name, barChoose: this.barChoose }, title, 'RightLeftTop');
    },
  },
};
</script>
<style lang="scss" scoped>
.box-bar {
  width: 100%;
  display: flex;
  z-index: 999;
  // justify-content: center;
  align-items: center;
  padding-left: 20px;

  .box {
    width: 56px;
    height: 20px;
    margin: 0 2px;
    text-align: center;
    line-height: 20px;
    font-size: 12px;
    cursor: pointer;
    color: #fff;
    z-index: 2;

    .mask {
      position: absolute;
      width: 56px;
      height: 20px;
      z-index: 1;
      background: rgba(255, 255, 255, 0.05);
      /* 对 div 进行水平倾斜 */

      &.actived {
        // background-color: #00D5FF;
        background-color: #154FC1;
        color: #061227;
        z-index: -1;
      }
    }
  }
}
</style>
